<?php

/**
 * NewsSet(业务管理---新闻管理)
 */
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
use Common\Model\ColumnLiveAdminModel;
use Common\Model\NewsColumnAdminModel;
use Common\Model\NewsColumnModel;

class NewsSetController extends AdminbaseController {

    protected $news_model;
    protected $news_column_model;

    function _initialize() {
        parent::_initialize();
        $this->news_model = D("Common/News");
        $this->news_column_model = D("Common/NewsColumn");
    }

    /**
     *  显示新闻栏目列表
     */
    public function defaultIndex() {
        $_SESSION['admin_newsset_index'] = "NewsSet/defaultIndex";

        $currentPage = I('param.p', 0);
        if ($currentPage <= 0) {
            $currentPage = 1;
        }

        $pageStart = ($currentPage - 1) * 20;


        $name = $_SESSION['name'];
        if($_SESSION['ADMIN_ID'] == 1){
            //超级管理员
            $status = 1;
            $name = "超级管理员";
            //查看所有栏目列表
            $column_list = $this->news_column_model->Getall($pageStart,20);

            foreach($column_list['list'] as $key=>$val){
                if($val['admin_ids']) {
                    $column_list['list'][$key]['admin_infos'] = array_combine(explode(',',$val['admin_ids']), explode(',',$val['usernames']));

                }
            }
//            print_r($column_list);die;

            //分页
            $page = $this->page($column_list['count'], 20);
            $this->assign("page", $page->show('Admin'));

            $this->assign("name", $name);
            $this->assign("data", $column_list['list']);
        }else{
            //普通管理员
            $status = 2;
            //查看 自己权限内的栏目列表
            $columns = $this->news_column_model->GetColumns($_SESSION['ADMIN_ID']);

            $areas = array();
            //查看 本地 栏目
            foreach ($columns as $k=>$v){
                if($v['name'] == '本地'){
                    unset($columns[$k]);
                    $area_model = M('vrnews1.area_admin',null);
                    $areas = $area_model
                        ->join("vrnews1.area on vrnews1.area_admin.area_id = vrnews1.area.area_id",'left')
                        ->where("admin_id = ".$_SESSION['ADMIN_ID'])->field("vrnews1.area_admin.area_id,vrnews1.area.name")->select();
                }
            }
            $res_columns = array_merge($columns,$areas);

            $this->assign("name", $name);
            $this->assign("data", $res_columns);
        }
        $this->assign("status", $status);
        $this->display('defaultindex');
    }

    //修改 栏目名\权重\管理员 (超级管理员不可更改)
    function Edit(){
        $type = $_POST['type'];
        $id   = $_POST['id'];
        $val  = $_POST['val'];
        $edit_col = $this->news_column_model->Edit($type,$id,$val);

        if($edit_col !== false){
            echo 1;
        }else{
            echo 2;
        }

    }

    //显示 修改弹窗
    public function editAdmin(){
        $column_id = I("column_id");
        $admin_ids = I("admin_ids");
        $usernames = I("usernames");

        if(empty($usernames)){
            $arr_usernames = '';
        }else{
            $arr_usernames = explode(',',$usernames);
        }
        if(empty($admin_ids)){
            $arr_admin_ids = '';
        }else{
            $arr_admin_ids = explode(',',$admin_ids);
        }


//        $arr_mer = array_combine($arr_admin_ids,$arr_usernames);

        //全部 管理员
        $admins = M("vradmin1.admin_user",null);
        $admin_infos = $admins
            ->where("type = 2 and status = 1")
            ->select();

        $this->assign("arr_admin_ids",$arr_admin_ids);
        $this->assign("arr_usernames",$arr_usernames);
        $this->assign("admin_infos",$admin_infos);
        $this->assign("admin_ids",$admin_ids);
        $this->assign("column_id",$column_id);
        $this->assign("usernames",$usernames);
        $this->display('editadmin');
    }

    //修改 管理员
    public function editAdminPost(){
        $result = '0';
        $column_id = I("param.column_id", "");
        $admin_ids = rtrim(I("param.admin_hidden_ids", ""),',');

        if (empty($column_id)) {
            echo $result;exit;
        }


        //设置管理员
        if($admin_ids){
            $admin_arr = explode(',', $admin_ids);

            foreach($admin_arr as $admin_key=>$admin_value){
                $dataList[] = array('column_id'=>$column_id,'admin_id'=>$admin_value);
                $dataAdminids[] = $admin_value;
            }

            $admin_model = new NewsColumnAdminModel();
            $columns_sel = $admin_model->where("column_id = $column_id")->select();

            if($columns_sel){
                $admin_model->where('column_id = '.$column_id)->delete();
            }
            if($admin_model->addAll($dataList)){
                //查看 对应的 直播权限管理员
                $live_admin = new ColumnLiveAdminModel();
                $live_admins = $live_admin->where("column_id = ".$column_id. " and type = 0")->select();
                //处理 对应的直播权限管理人
                foreach ($live_admins as $key=>$val){
                    if(!in_array($val['admin_id'], $dataAdminids)){
                        $live_admin->where("id = ".$val['id'])->delete();
                    }
                }
            }
                echo 1;
        }else{
            echo '0';
        }

    }

    //显示 修改直播权限 弹窗
    public function editliverole(){
        $column_id = I("column_id");
        $admin_ids = I("admin_ids");
        $usernames = I("usernames");
        $live_status = I("live_status"); //0关闭 1 开启
        $type = I("type"); //0常规栏目 1本地栏目

        $id_array = explode(',',$admin_ids );
        $name_array = explode(',',$usernames );
        //栏目 有管理权限的全部人
        $arr_mer = array_combine($id_array,$name_array);

        //查看 对应 直播管理权限
        $live_column_admins = new  ColumnLiveAdminModel();
        $live_admins = $live_column_admins->alias("a")
            ->join("vradmin1.admin_user b on a.admin_id = b.admin_id")
            ->where("a.column_id = $column_id and a.type = $type")
            ->field("a.admin_id,b.username")
            ->select();

        //当前 拥有直播权限的人
        foreach ($live_admins as $key=>$val){
            $live_arr[$key] = $val['username'];
            $live_ids_arr[$key] = $val['admin_id'];
        }

        //关闭状态 为空
        if($live_status == 0){
            $live_arr = '';
            $live_ids_arr = '';
        }

        $this->assign("live_arr",$live_arr);
        $this->assign("column_id",$column_id);
        $this->assign("live_status",$live_status);
        $this->assign("arr_mer",$arr_mer);
        $this->assign("live_ids_arr",$live_ids_arr);
        $this->assign("column_type",$type);
        $this->display('editliverole');
    }
    
    //修改直播权限
    public function liverolePost(){
        $result = '0';
        $column_id = I("param.column_id", "");
        $live_status = I("param.live_status");
        $column_type = I("param.column_type"); //0常规，1本地
        $admin_ids = rtrim(I("param.admin_hidden_ids", ""),',');

        if (empty($column_id)) {
            echo $result;exit;
        }
        if($live_status == 0){
            //关闭
            //修改 栏目直播状态
            if($column_type == 0){ //常规
                $news_column = new NewsColumnModel();
                $data['live_status'] = 0;
                $news_column->where('column_id = '.$column_id)->data($data)->save();
            }
            $admin_model = new ColumnLiveAdminModel();
            $columns_sel = $admin_model->where("column_id = $column_id and type = $column_type")->select();

            if($columns_sel){
                $admin_model->where('column_id = '.$column_id.' and type = '.$column_type)->delete();
                echo 1;
            }else{
                echo $result;
            }
        }else{
            //开启
            //修改 栏目直播状态
            if($column_type == 0){ //常规
                $news_column = new NewsColumnModel();
                $data['live_status'] = 1;
                $news_column->where('column_id = '.$column_id)->data($data)->save();
            }

            //设置管理员
            if($admin_ids){
                $admin_arr = explode(',', $admin_ids);

                foreach($admin_arr as $admin_key=>$admin_value){
                    $dataList[] = array('column_id'=>$column_id,'admin_id'=>$admin_value,'type'=>$column_type);
                }

                $admin_model = new ColumnLiveAdminModel();
                $columns_sel = $admin_model->where("column_id = $column_id and type = $column_type")->select();

                if($columns_sel){
                    $admin_model->where('column_id = '.$column_id.' and type = '.$column_type)->delete();
                }
                if($admin_model->addAll($dataList)) echo 1;
            }else{
                echo '0';
            }
        }

    }

    //删除 栏目
    public function Delete(){
        $column_id = I('param.id', "");
        if (empty($column_id)) {
            $this->error("参数错误！");
        }
        $result = $this->news_column_model->colDel($column_id);
        if ($result) {
            $this->success("删除成功！");
        } else {
            return $this->error($result['Message']);
        }
    }

    //到添加页面
    public function add(){
        $_SESSION['admin_newsset_add'] = "NewsSet/add";
        //全部 管理员
        $admins = M("vradmin1.admin_user",null);
        $admin_infos = $admins
            ->where("type = 2 and status = 1")
            ->select();

        $this->assign("admin_infos",$admin_infos);
        $this->display();
    }

    //添加 栏目
    public function addColumn(){
        $type = I("param.type","");
        $column_name = I('param.column_name',"");
        $weight = I("param.weight","");
        $admin_ids = rtrim(I("admin_hidden_ids",''),',');


        if($type == 1){
            //常规栏目 添加
            $data['name']   = $column_name;
            $data['type']   = $type;
            $data['weight'] = $weight;
            $data['create_time'] = date("Y-m-d H:i:s");
            $data['create_id']   = $_SESSION['ADMIN_ID'];
            $data['status']      = 1;


            $column_model = new NewsColumnModel();
            $res_id = $column_model->add($data);

            //刚添加的栏目ID $res_id

            //设置管理员
            if($admin_ids){
                $admin_arr = explode(',',$admin_ids);
                $column_admin_model = new NewsColumnAdminModel();
                foreach($admin_arr as $admin_key=>$admin_value){
                    $dataList[] = array('column_id'=>$res_id,'admin_id'=>$admin_value);
                }
                $res_admid = $column_admin_model->addAll($dataList);
            }

            if (!empty($res_id)) {
                $successInfo = "添加成功";
                $this->success($successInfo, U("Admin/NewsSet/defaultIndex"));
            } else {
                $this->error($res_id['Message'] ? $res_id['Message'] : $res_admid['Message']);
            }
        }else{
            //添加 本地栏目
            $data['name']   = $column_name;
            $data['type']   = $type;
            $data['weight'] = $weight;
            $data['create_time'] = date("Y-m-d H:i:s");
            $data['create_id'] = $_SESSION['ADMIN_ID'];
            $data['status']    = 1;
            $column_model = new NewsColumnModel();
            $res_id = $column_model->add($data);

            if (!empty($res_id)) {
                $successInfo = "添加成功";
                $this->success($successInfo, U("Admin/NewsSet/defaultIndex"));
            } else {
                $this->error($res_id['Message']);
            }
        }


    }



}